using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Acms.Bll;
using Telerik.Web.UI;
using System.Collections.Generic;
using Acms.AcmsControls;
using Acms.AcmsControls.ContentControls.FrontEnd;

public partial class UserGeneratedContent_Uploads : BaseUserGeneratedContentPage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer, Acms.Bll.Role.Roles.UserGeneratedContentEditor, Acms.Bll.Role.Roles.ContentEditor, Acms.Bll.Role.Roles.ContentViewer }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (Acms.WebControls.DownloadGridField field in dgUploads.Columns)
            {
                if (field.DataField == "FileId")
                {
                    string serverName = Page.Request.ServerVariables["Server_Name"];
                    string port = Request.ServerVariables["SERVER_PORT"];
                    if (port == null || port == "80" || port == "443")
                        port = "";
                    else
                        port = string.Concat(":", port);
                    field.Format = string.Concat("http://", serverName, port, Page.ResolveUrl("~/File.aspx"), "?Id={0}");
                    break;
                }
            }
            if (Post != null)
            {
                dgUploads.FileName = string.Format("{0} Uploads", base.Post.DisplayName);
            }
        }
    }

    protected void thumbImage2_DataBound(object sender, EventArgs e)
    {
        AcmsImageLink link = sender as AcmsImageLink;
        if (link != null)
        {
            link.NavigateUrl = string.Format("~/File.aspx?Id={0}&width=600&height=600", link.FileId);
        }
    }

    protected void dsUploads_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UserPostUpload> e)
    {
        List<UserPostUpload> uploads = UserPostUpload.GetAllByPostId(PostId);
        UserPostUpload.Sort(uploads, e.SortExpression);
        e.SetData(uploads);
    }

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if ((e.CommandName == "Approve" || e.CommandName == "Suspend") && e.CommandArgument != null)
        {
            int index = -1;
            try
            {
                index = int.Parse((string)e.CommandArgument);
            }
            catch
            {
            }
            if (index > -1)
            {
                int id = (int)RadGrid1.MasterTableView.DataKeyValues[index]["Id"];
                object concurencyId = RadGrid1.MasterTableView.DataKeyValues[index]["ConcurencyId"];
                UserPostUpload upload = UserPostUpload.GetById(id);
                if (upload != null)
                {
                    upload.CheckConcurent(concurencyId);
                    if (e.CommandName == "Approve")
                    {
                        upload.Approve();
                    }
                    else if (e.CommandName == "Suspend")
                    {
                        upload.Suspend();
                    }
                    upload.Update();
                }
                RadGrid1.Rebind();
            }
        }
    }
}
